﻿using PIMS.Base;
using PIMS.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PIMS.WorkPlugin.Util
{
    internal class PLCSignalHandlePlus
    {
        public delegate void MsgHandler(PlcSignalPara para);

        private IDebugMsg debug = null;

        private ILog4net log = null;

        private int _itemId = -1;

        private EventId _eventId;

        private bool signalUp = false;

        private bool signalDown = false;

        private bool readFault = false;

        private bool firstRun = true;

        public bool EventBindOk => this.msgHandler != null;

        public event MsgHandler msgHandler;

        public PLCSignalHandlePlus(int itemId, EventId eventId, IDebugMsg debugMsg, ILog4net log4net)
        {
            _itemId = itemId;
            _eventId = eventId;
            debug = debugMsg;
            log = log4net;
        }

        public bool CheckSignal(ICommunication plc, int plcIndex)
        {
            bool result = false;
            if (plc != null && plc.IsConnected)
            {
                bool result2;
                bool flag = plc.Read<bool>(_itemId, out result2);
                if (result2)
                {
                    readFault = false;
                    if (flag)
                    {
                        firstRun = false;
                        signalDown = false;
                        if (!signalUp)
                        {
                            string text = $"+ 收到“{plc.GetDeviceName()}：{plc.GetItemName(_itemId)}”信号！";
                            debug.DebugMsg(text);
                            log.Info(text);
                            if (this.msgHandler != null)
                            {
                                PlcSignalPara plcSignalPara = new PlcSignalPara();
                                plcSignalPara.plc = plc;
                                plcSignalPara.plcIndex = plcIndex;
                                plcSignalPara.eventId = _eventId;
                                this.msgHandler(plcSignalPara);
                            }

                            signalUp = true;
                        }
                    }
                    else
                    {
                        if (firstRun)
                        {
                            firstRun = false;
                            signalDown = true;
                        }

                        signalUp = false;
                        if (!signalDown)
                        {
                            string text2 = $"-“{plc.GetDeviceName()}：{plc.GetItemName(_itemId)}”信号复位！";
                            debug.DebugMsg(text2);
                            log.Info(text2);
                            signalDown = true;
                        }
                    }
                }
                else if (!readFault)
                {
                    string text3 = $"“{plc.GetDeviceName()}：{plc.GetItemName(_itemId)}”信号读取失败！信号ID={_itemId}";
                    debug.DebugMsg(text3, InfoLevel.error);
                    log.Info(text3);
                    readFault = true;
                }
            }

            return result;
        }
    }
}
